package org.example.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.reggie.common.BaseContext;
import org.example.reggie.entity.Dish;
import org.example.reggie.entity.OrderDetail;
import org.example.reggie.entity.Orders;
import org.example.reggie.entity.User;
import org.example.reggie.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.example.reggie.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Resource
    private UserService userService;
    @Resource
    private DishService dishService;
    @Resource
    private OrderService orderService;
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private CategoryService categoryService;

    @Resource
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public void payment(long[] ids) {
        //      获取当前用户id
        Long userid = BaseContext.getCurrentId();
        //查询用户数据
        User user = userService.getById(userid);
        //查询地址数据
        if (user.getAddress() == null) {
            throw new RuntimeException("用户地址信息有误，不能下单");
        }
        //订单号
        long orderId = IdWorker.getId();

        //金额
        BigDecimal sum=new BigDecimal("0.0");

        for (long id : ids) {
            if (id==0){
                continue;
            }
            Dish dish=dishService.getById(id);
            OrderDetail orderDetail=new OrderDetail();
            sum=sum.add(dish.getPrice().multiply(BigDecimal.valueOf(dish.getNumber())));
            //生成随机号
            long orderDetailId = IdWorker.getId();

            orderDetail.setId(orderDetailId);
            orderDetail.setName(user.getName());
            orderDetail.setImage(dish.getImage());
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(dish.getId());
            orderDetail.setDishFlavor(null);
            orderDetail.setSetmealId(null);
            orderDetail.setNumber(dish.getNumber());
            orderDetail.setAmount(dish.getPrice());

            orderDetailService.save(orderDetail);
        }

        //封装数据
        Orders orders = new Orders();
        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId));
        orders.setStatus(1);
        orders.setUserId(userid);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now().plusMinutes(30));
        orders.setPayMethod(1);
        orders.setAmount(sum);
        orders.setRemark(null);
        orders.setPhone(user.getPhone());
        orders.setAddress(user.getAddress());
        orders.setUserName(user.getName());
        orders.setConsignee(user.getName());
        //保存
        orderService.save(orders);
    }

    @Override
    public Orders updateStatus(Orders order){
        UpdateWrapper<Orders> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", order.getNumber());
        log.info("order.getId():"+order.getNumber());
        log.info("order.getStatus():"+order.getStatus());
        updateWrapper.set("status",order.getStatus());
        orderMapper.update(null, updateWrapper);

        return orderService.getById(order.getNumber());
    }

}
